/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: GodK
 * Date: 2024-07-22
 * Time: 19:56
 */
public class Test {
    public boolean[][] flag;
    public int[] dx = {1,-1,0,0};
    public int[] dy = {0,0,1,-1};
    public int count = 0;
    public int solve (char[][] grid) {
        int n = grid.length;
        int m = grid[0].length;
        flag = new boolean[n][m];
        for(int i = 0;i < n;i++)
            for(int j = 0;j < m;j++){
                if(grid[i][j] == 1 && !flag[i][j]){
                    flag[i][j] = true;
                    func(grid,i,j);
                    count++;
                }
            }
        return count;
    }
    public void func (char[][] grid,int i,int j){
        for(int k = 0;k < 4;k++){
            int x = dx[k] + i;
            int y = dy[k] + j;
            if(x >= 0 && x < grid.length && y >= 0 && y < grid[0].length && !flag[x][y] && grid[x][y] == 1){
                flag[x][y] = true;
                func(grid,x,y);
            }
        }
    }
    public static void main(String[] args) {
        Test test = new Test();
        char[][] ch = {{1,1,0,0,0}, {0, 1, 0, 1, 1}, {0, 0, 0, 1, 1}, {0, 0, 0, 0, 0}, {0, 0, 1, 1, 1}};
        System.out.println(test.solve(ch));
    }
}
